#include  "Ringqueue.hpp"
#include <unistd.h>

Ring::Ringqueue<int> q;

int count = 0;

struct RQ
{
    Ring::Ringqueue<int>* ptr;
    std::string s;
};

void* Product(void* argv)
{
    std::string s = static_cast<char*>(argv);

    while(true)
    {
        q.Enqueue(count);
        std::cout << s << "生产了一个数字" << count << std::endl;
        ++count;
        sleep(1);
    }

    return nullptr;
}

void* Consume(void* argv)
{
    std::string s = static_cast<char*>(argv);

    while(true)
    {
        int ret = q.Pop();
        std::cout << s << "消费了一个数字" << ret << std::endl;
        sleep(1);
    }

    return nullptr;
}

int main()
{

    pthread_t c[2],p[2];

    pthread_create(p,nullptr,Product,(void*)"生产1");
    pthread_create(p + 1,nullptr,Product,(void*)"生产2");

    pthread_create(c,nullptr,Consume,(void*)"消费1");
    pthread_create(c + 1,nullptr,Consume,(void*)"消费2");

    pthread_join(c[0],nullptr);
    pthread_join(c[1],nullptr);

    pthread_join(p[0],nullptr);
    pthread_join(p[1],nullptr);

    return 0;
}